//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using Shp.Runtime.Contract;

namespace LitwareHR.Portfolio.Gateways
{
    static public class AuthenticationGateway
    {
        static public void CreateRole(string tenantAlias, string roleName)
        {
            using (SecureChannel channel = new SecureChannel())
            {
                channel.CreateRole(tenantAlias, roleName);
            }
        }

        static public bool DeleteRole(string tenantAlias, string roleName)
        {
            using (SecureChannel channel = new SecureChannel())
            {
                return channel.DeleteRole(tenantAlias, roleName);
            }
        }

        static public bool ValidateUser(string tenantAlias, string user, string password)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.ValidateUser(tenantAlias, user, password);
            }
        }

        static public PasswordPolicy GetPasswordPolicy()
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetPasswordPolicy();
            }
        }

        static public bool IsUser(string tenantAlias, string user)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.IsUser(tenantAlias, user);
            }
        }

        static public string[] GetUsers(string tenantAlias)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetUsers(tenantAlias);
            }
        }

        #region Roles
        static public void AddUserToRole(string tenantAlias, string username, string roleName)
        {
            using (SecureChannel channel = new SecureChannel())
            {
                channel.AddUserToRole(tenantAlias, username, roleName);
            }
        }

        static public void RemoveUserFromRole(string tenantAlias, string username, string roleName)
        {
            using (SecureChannel channel = new SecureChannel())
            {
                channel.RemoveUserFromRole(tenantAlias, username, roleName);
            }
        }

        static public string[] GetRoles(string tenantAlias)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetRoles(tenantAlias);
            }
        }

        static public bool IsUserInRole(string tenantAlias, string username, string roleName)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.IsUserInRole(tenantAlias, username, roleName);
            }
        }

        static public string[] GetRolesForUser(string tenantAlias, string user)
        {
            using (UnSecureChannel channel = new UnSecureChannel())
            {
                return channel.GetRolesForUser(tenantAlias, user);
            }
        }

        #endregion

        private class SecureChannel : ClientBase<IAuthenticationSC>, IAuthenticationSC
        {
            public void AddUserToRole(string tenantAlias, string username, string roleName)
            {
                base.Channel.AddUserToRole(tenantAlias, username, roleName);
            }

            public void CreateRole(string tenantAlias, string roleName)
            {
                base.Channel.CreateRole(tenantAlias, roleName);
            }

            public bool DeleteRole(string tenantAlias, string roleName)
            {
                return base.Channel.DeleteRole(tenantAlias, roleName);
            }

            public void RemoveUserFromRole(string tenantAlias, string username, string roleName)
            {
                base.Channel.RemoveUserFromRole(tenantAlias, username, roleName);
            }
        }

        private class UnSecureChannel : ClientBase<IAuthenticationUC>, IAuthenticationUC
        {
            public bool ValidateUser(string tenantAlias, string user, string password)
            {
                return base.Channel.ValidateUser(tenantAlias, user, password);
            }

            public PasswordPolicy GetPasswordPolicy()
            {
                return base.Channel.GetPasswordPolicy();
            }

            public bool IsUser(string tenantAlias, string user)
            {
                return base.Channel.IsUser(tenantAlias, user);
            }

            public string[] GetRoles(string tenantAlias)
            {
                return base.Channel.GetRoles(tenantAlias);
            }

            public string[] GetUsers(string tenantAlias)
            {
                return base.Channel.GetUsers(tenantAlias);
            }

            public string[] GetRolesForUser(string tenantAlias, string user)
            {
                return base.Channel.GetRolesForUser(tenantAlias, user);
            }

            public bool RoleExists(string tenantAlias, string roleName)
            {
                return base.Channel.RoleExists(tenantAlias, roleName);
            }

            public bool IsUserInRole(string tenantAlias, string username, string roleName)
            {
                return base.Channel.IsUserInRole(tenantAlias, username, roleName);
            }
        }
    }
}
